// pages/store/store.js
const app = getApp()

Page({

  data: {
    shop :{
      city     : "",
      iscode   : 0,
      iscollect: "",
      _num     : 0,
      title    : "",
      quantity : "",
      phone    : "",
      img      : "",
      shoptitle: "",
      desc     : "",
      range    : "",
      hassale  : "",
      evaluate : "",
    },
    goods_list:[
      {
        // src:"../../img/a_68_03.png",
        // name:"机油"
      }
    ],
    details_list:[
      {
        // src:"../../img/a_60_03.png",
        // name:"飞利浦（PHILIPS）迷你车载充电器快充01",
        // stock:"5",
        // price:"69.0"
      }
    ]
  },
  chose_city: function () {
    wx.navigateTo({ url: '../address/address'})
  },
  collect: function (e) {
    let that   = this
    var iscode = that.data.iscode;
    var uid    = wx.getStorageSync('uid');
    var bid    = e.currentTarget.id;
    wx.request({
      url     : app.url + '/Business/add_collect',
      data    : { uid : uid,bid : bid, type : 0},
      method  : 'post',
      success : function (res) {
        var code = res.data.res.code;
        if (code == 1) {
          wx.showToast({ title: '收藏成功', })
          that.setData({
            
          })
        } else if (code == 2) {
          wx.showToast({ title: '取消收藏', })
          that.setData({
            
          })
        } else {
          wx.showToast({ title: '操作失败', })
        }
      }
    })
  },
  focus_click: function (e) {
    let that = this;
    var uid  = wx.getStorageSync('uid');
    var cid  = e.target.id;
    var bid  = e.target.dataset.bid;
    wx.showLoading({ title: '加载中' })
    wx.request({
      url     : app.url + '/business/cate_goods',
      method  : 'post',
      data    : {uid : uid, cid : cid, bid : bid},
      success : function(res){
        if(res.data.code == 1){
          that.setData({
            details_list : res.data.data,
            _num         : e.target.dataset.num,
            title        : e.target.dataset.title
          })
        }
      },
      error   : function(){}
    })
    // that.setData({
    //   _num  : e.target.dataset.num,
    //   title : e.target.dataset.title
    // })
    wx.hideLoading();
  },
  // 机油点击事件
  engine_click:function(){
    let that = this;
    wx.showLoading({ title: '加载中', })
    that.setData({ title: "机油" })
    wx.hideLoading()
  },
  // 拨打电话
  call: function (e) {
    wx.makePhoneCall({
      phoneNumber: e.currentTarget.dataset.phone, //此号码并非真实电话号码，仅用于测试
      success: function (){},
      fail   : function (){}
    })
  },
  // 一键导航
  goto: function () {
    wx.openLocation({
      latitude : 23.362490,
      longitude: 116.715790,
      scale    : 18,
      name     : '华乾大厦',
      address  : '金平区长平路93号'
    })
  },
  // 点击进入门店详情页
  storeurl:function(){
    wx.navigateTo({ url: '../storeDetails/storeDetails',})
  },
  // 点击进入详情页
  goodsurl:function(e){
    var gid = e.target.id;
    var bid = e.target.dataset.bid;
    wx.navigateTo({ url: '../goodsDetails/goodsDetails?gid=' + gid + '&types=1&bid=' + bid})
  },
  // 立即购买
  buynow:function(e){
    var gid = e.target.id;
    var bid = e.target.dataset.bid;
    wx.navigateTo({ url: '../goodsDetails/goodsDetails?gid=' + gid + '&types=1&bid=' + bid })
  },
  onLoad: function (options) {
    let that = this
    that.setData({
      quantity:that.data.details_list.length
    })
    var longitude = 108.94712;
    var latitude  = 34.29318;
    wx.getLocation({
      type: 'wgs84',
      success: function (res) {
        latitude  = res.latitude
        longitude = res.longitude
        console.log(latitude, longitude)
      }
    });
    var uid = wx.getStorageSync('uid');
    var bid = options.bid;
    wx.request({
      url     : app.url + '/Business/shop_goods',
      method  : 'post',
      data: { uid: uid, bid: bid, longitude: longitude, latitude: latitude},
      success : function (res) {
        if(res.data.data.code == 1){
          that.setData({
            goods_list  : res.data.data.type,
            details_list: res.data.data.data,
            shop        : res.data.data.shop
          })
          that.setData({ show: true })
          
        }else if(res.data.data.code == 2 || res.data.data.code == 3) {
          that.setData({ shop: res.data.data.shop })
          wx.showToast({ title: '暂无任何商品', duration: 1500 })
          that.setData({show :false})
        }else{
          wx.showToast({ title: '暂无店铺信息',duration: 1500 })
        }
      },
      error: function () { }
    })
  },
  onReady: function () {
  
  },
  onShow: function () {
    let that = this
    that.setData({
      city  : app.data.address.name || "西安",
      isboss: app.data.isboss
    })
  },
  onHide: function () {
  
  },
  onUnload: function () {
  
  },
  onPullDownRefresh: function () {
  
  },
  onReachBottom: function () {
  
  },
  onShareAppMessage: function () {
  
  }
})